﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Caching;
using System.Web.Security;
using System.Text;

namespace BiliBiliiMG.Controllers
{
    public class OtherController : Controller
    {
        #region 页面
        // GET: Error
        public ActionResult Error()
        {
            return View();
        }

        public ActionResult TestCache()
        {
            return View();
        }

        public ActionResult VideoCrx() {

            return View();

        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="nmb">url</param>
        /// <param name="mmp">md5</param>
        /// <returns></returns>
        public ActionResult VideoUrl(string nmb , string mmp )
        {
            //md5加密当天日期当前时间点0分0秒的时间戳，否则不通过

            //第一步：获取指定字符串中的所有字符编码为一个字节序列;
            //byte[] byteResult = md5.ComputeHash(Encoding.UTF8.GetBytes(str));//获取加密后的序列
            //第二步： 将指定的字节数组的每个元素的数值转换为它的等效十六进制字符串表示形式，这里转换出来的十六进制字符串是"A8-F5-F1-67-F4-4F-49-64-E6-C9-98-DE-E8-27-11-0C"这样的，
            //string strResult = BitConverter.ToString(byteResult);//转换为十六进制
            //第三步，去掉"-"符号
            //string realResult = strResult.Replace("-", null);
            
            System.Security.Cryptography.MD5 md5 = System.Security.Cryptography.MD5.Create();
            var datenowMd5 = BitConverter.ToString(md5.ComputeHash(Encoding.UTF8.GetBytes("bilibiliup.cn"))).Replace("-", null);
            if (mmp.Trim().ToUpper()== datenowMd5)
            {
                if (!String.IsNullOrWhiteSpace(nmb))
                {
                    ViewData["url"] = nmb;
                }
                else
                {
                    ViewData["url"] = "false";
                }
                return View();
            }
            else
            {
                return null;
            }
        }
        #endregion

        #region 方法

        public ContentResult Caches(string text = "")
        {
            if (string.IsNullOrEmpty(text))
            {

                return Content(HttpContext.Cache["content"].ToString());
            }
            else
            {
                HttpContext.Cache["content"] = text;
                HttpContext.Cache.Insert("contexts", text, null, DateTime.Now.AddMinutes(1), Cache.NoSlidingExpiration);
            }
            return Content(HttpContext.Cache["content"].ToString());
        }
        #endregion
    }
}